package com.wxh.excel;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ExcelController
 * @Description: Excel控制器类
 * @Author wxh
 * @Date: 2021/12/3 14:59
 * @Version V1.0.0
 * @Since 1.8
 */
@RestController
@RequestMapping("")
public class ExcelController {

    @Resource
    private ExcelService excelService;

    @RequestMapping(value = "/exportToExcel", method = RequestMethod.POST)
    public void exportToExcel(@RequestBody JSONObject params, HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> paramList = (List<Map<String, Object>>) params.get("data");
        excelService.exportToExcel(paramList, response);
    }


    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public void importExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        excelService.importExcel(file, request, response);
    }

    @RequestMapping(value = "/excel/{fileName}", method = {RequestMethod.POST,RequestMethod.GET})
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response, @PathVariable("fileName") String fileName) {
        excelService.downloadExcel(request, response,  fileName);
    }
}
